//
//  HdwILdJCScanResultViewController.swiftNbqyGZ
//  QRScan
//
//  Created by Qiang on 2018/3/26.
//  Copyright © 2018年 Qiang. All rights reserved.
//

import UIKit

class XNErwDScanViewController: JGayoKJCBaseViewController {

    let textView = UITextView()
    let showURLButton = UIButton()
    
    var urlString = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.title = "scan result"
        self.textView.frame = CGRect.init(x: 20, y: 100, width: view.frame.size.width-40, height: 200)
        self.textView.textColor = UIColor.black
        self.view .addSubview(self.textView)
        self.textView.text = self.urlString
        self.textView.layer.masksToBounds = true
        self.textView.layer.cornerRadius = 5
        self.textView.layer.borderColor = UIColor.brown.cgColor
        self.textView.layer.borderWidth = 0.5
        
        self.showURLButton.frame = CGRect.init(x: 20, y: self.textView.frame.size.height+120, width: self.textView.frame.size.width, height: 40)
        self.showURLButton .addTarget(self, action: #selector(showUrl), for: UIControlEvents.touchUpInside)
        self.showURLButton .setTitleColor(UIColor.black, for: UIControlState.normal)
        self.showURLButton .setTitle("open website?", for: UIControlState.normal)
        self.view .addSubview(self.showURLButton)
        self.showURLButton.isHidden = true
        
        
        if NSURL(string: self.urlString) != nil{
            self.showURLButton.isHidden = false
        }

        // Do any additional setup after loading the view.
    }
    private func validateURL()->Bool{
        do {
            let pattern = "[a-zA-z]+://[^\\s]*"
            let regex: NSRegularExpression = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
            let matches = regex.matches(in: self.urlString, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, self.urlString.characters.count))
            return matches.count > 0
        } catch  {
            return false
        }

    }

    @objc func showUrl() {
        
        //let sb = UIStoryboard(name: "Main", bundle: nil)
        let vc = WWXViewController()
        vc.webUrl = urlString
        self.pushViewController(viewController: vc, animated: true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
